/**
 * Created by Administrator on 2016/7/14.
 */
"use strict";
swan_pc
    .controller("LookOverCalShowController",function($q,$scope,$rootScope,UserClip,TimeExchange,$timeout,$state,Clip,GetAccountInfo){
        //console.log("LookOverCalShowController is run!!!");
        $scope.initCalendar=function(){
            var date = new Date();
            var d = date.getDate();
            var m = date.getMonth();
            var y = date.getFullYear();

            var calendar = $('#calendar').fullCalendar({
                header: {
                    left: 'prev,next today',
                    center: 'title',
                    right: 'month,agendaWeek,agendaDay'
                },
                selectHelper: true,
                select: function(start, end, allDay) {
                    var title = prompt('Event Title:');
                    if (title) {
                        calendar.fullCalendar('renderEvent',
                            {
                                title: title,
                                start: start,
                                end: end,
                                allDay: allDay
                            },
                            true // make the event "stick"
                        );
                    }
                    calendar.fullCalendar('unselect');
                },
                droppable: true, // this allows things to be dropped onto the calendar !!!
                drop: function(date, allDay) { // this function is called when something is dropped

                    // retrieve the dropped element's stored Event Object
                    var originalEventObject = $(this).data('eventObject');

                    // we need to copy it, so that multiple events don't have a reference to the same object
                    var copiedEventObject = $.extend({}, originalEventObject);

                    // assign it the date that was reported
                    copiedEventObject.start = date;
                    copiedEventObject.allDay = allDay;
                    // is the "remove after drop" checkbox checked?
                    if ($('#drop-remove').is(':checked')) {
                        // if so, remove the element from the "Draggable Events" list
                        $(this).remove();
                    }
                },
                /*eventClick:function(event,jsEvent,view){
                    $rootScope.presentEventid=event.id;
                    $state.go("clipdetail");
                },*/
                dayClick:function(date, allDay, jsEvent, view){
                    $rootScope.clickedDate = date;
                    $state.go("lookoverAddClip");
                }
            })
        }
        $scope.initCalendar();
        $scope.loadAll=function(){
            //console.log($scope.$parent.userClipList);
            angular.forEach($scope.$parent.userClipList, function (data, index, array) {
                //console.log($scope.$parent.userClipList);
                if(data.level!=null) {
                    var clipLevel = data.level.toString();
                }
                //console.log(data.startTime);
                //console.log(data.endTime);
                var startDate = new Date(data.startTime);
                var endDate = new Date(data.endTime);
                //console.log("clipDetail:"+data.name+startDate+clipLevel);
                if(clipLevel == undefined && data.status=="CONFIRM"){
                    var events = [
                        {
                            title: "此处已被占用",
                            start: startDate,
                            end: endDate,
                            allDay: false,
                            backgroundColor: Theme.colors.gray,
                            id: data.id
                        }
                    ];
                }
                if (clipLevel == "主事项" && data.status == "CONFIRM") {
                    if ($scope.$parent.userClipListShowFlag) {
                        var events = [
                            {
                                title: data.name,
                                start: startDate,
                                end: endDate,
                                allDay: false,
                                backgroundColor: Theme.colors.blue,
                                id: data.id
                            }
                        ];
                    } else {
                        var events = [
                            {
                                title: "此处已被占用",
                                start: startDate,
                                end: endDate,
                                allDay: false,
                                backgroundColor: Theme.colors.gray,
                                id: data.id
                            }
                        ];
                    }
                } else if (clipLevel == "主事项" && data.bookerId == $scope.MyId) {
                    if(data.status=="DRAFT") {
                        var events = [
                            {
                                title: data.name,
                                start: startDate,
                                end: endDate,
                                allDay: false,
                                backgroundColor: Theme.colors.red,
                                id: data.id
                            }
                        ];
                    }else if(data.status=="CANCEL"){
                        var events = [
                            {
                                title: data.name,
                                start: startDate,
                                end: endDate,
                                allDay: false,
                                backgroundColor: Theme.colors.gray,
                                id: data.id
                            }
                        ];
                    }
                }
                $("#calendar").fullCalendar("addEventSource", events);
            })
        }
        var def=$q.defer();
        var pro=def.promise;
        GetAccountInfo.get(function(result){
                $scope.presentAccountInfo=result;
                $scope.MyId=$scope.presentAccountInfo.id;
                def.resolve();
            },
            function(result){
                def.reject();
            })
        pro.then(function() {
                $scope.loadAll();
            }
        )
        $scope.$watch(function(){
            return $scope.$parent.userClipList;
            },
            function(newValue,oldValue, scope){
                //console.log("bianbianbian");
                if($rootScope.toState.name="lookoveruserclip") {
                    $("#calendar").fullCalendar("removeEvents");
                    if($scope.MyId!=undefined) {
                        $scope.loadAll();
                    }
                }
            });
    })